import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart


if __name__ == "__main__":
    # 邮件服务器配置
    smtp_server = 'smtp.qq.com'
    smtp_port = 587  # QQ 邮箱的 TLS 端口
    smtp_username = '195505183@qq.com'
    smtp_password = 'mqjcmynbbkvfbicf'

    # 收件人和发件人信息
    sender_email = '195505183@qq.com'
    receiver_email = '596744967@qq.com'
    subject = '邮件主题'

    # 创建邮件对象
    msg = MIMEMultipart()
    msg['from'] = sender_email
    msg['To'] = receiver_email
    msg['Subject'] = subject

    # 添加邮件正文
    body = '这是邮件正文内容'
    msg.attach(MIMEText(body, 'plain'))

    try:
        # 创建 SMTP 连接
        smtp = smtplib.SMTP(smtp_server, smtp_port)
        smtp.starttls()  # 启用 TLS 加密连接

        # 登录邮箱
        smtp.login(smtp_username, smtp_password)

        # 发送邮件
        smtp.sendmail(sender_email, receiver_email, msg.as_string())
        print('邮件发送成功')
    except Exception as e:
        print('邮件发送失败:', str(e))
    finally:
        smtp.quit()
